import asyncio
import random
import time

# doc https://docs.python.org/zh-cn/3.9/library/asyncio-task.html#coroutine

async def main():
    async def my_future():
        print('task/future started...')
        await asyncio.sleep(2)
        # await asyncio.sleep(2)
        print('task/future overed...')
        return 'TIGER'
    
    task = asyncio.ensure_future(my_future())  # 此时task就已经在执行了
    print('task->', type(task))

    # task.set_result(12)  # task 不能set_result 只能通过async函数返回
    # print(task.result())  如果task没有result 调用此方法出错

    # 如果task还没有执行完收到就停止了 那么会触发cancelled
    task.add_done_callback(lambda x: print('task over; ', x.result() if not x.cancelled() else 'task cancelled'))

    loop = asyncio.get_running_loop()
    fut = loop.create_future()
    fut.add_done_callback(lambda x: print('fut over; ', x, x.result()))
    fut.set_result(23)

    print(fut.result())


if __name__ == '__main__':
    asyncio.run(main())

